<?php

namespace App\Models\Factory;

use App\Helpers\DateUtils;
use App\Helpers\Generator\TokenGenerator;
use App\Helpers\Logger\SLogger;
use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\User;
use App\Models\Orm\UserBanks;
use App\Models\Orm\UserCertify;
use App\Models\Orm\UserIdentity;
use App\Models\Orm\UserInvite;
use App\Models\Orm\UserInviteCode;
use App\Models\Orm\UserProfile;
use App\Models\Orm\Identity;
use App\Strategies\SexStrategy;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

/**
 * 用户工厂
 * Class UserFactory
 * @package App\Models\Factory
 */
class UserFactory extends AbsModelFactory
{


    /**
     * 通过手机号查询用户信息
     */
    public static function getUserByMobile($mobile)
    {
        $user = User::where(['userphone' => $mobile])->first();
        return $user ? : false;
    }

    /**
     * 生成的token保存进用户表
     */
    public static function addUserTokenByMobile($mobile,$token)
    {
        $user = User::where(['userphone' => $mobile])->update($token);
        return $user;
    }

    /**
     * 获取所有vip用户
     */
    public static function allVipUser()
    {
        $vip = User::where(['vip_level' => 1])->get()->toArray();
        return $vip ? : [];
    }

    /*
     * 修改登录时间
     *
     */
    public static function addUserLassTime($mobile)
    {
        $time['lass_time'] = DateUtils::getToday();
        $user = User::where(['userphone' => $mobile])->update($time);
        return $user;
    }


    /*
     * 获取最后登录时间
     *
     */
    public static function getUserLassTime($mobile)
    {

        $lass_time = User::select('lass_time')->where(['userphone' => $mobile])->first();
        return $lass_time->lass_time;
    }

    /**
     * 通过token查找用户对象
     *
     * @param $token
     * @return mixed
     */
    public static function getUserByToken($token)
    {
        return User::where('token', $token)->first();
    }

    /**
     * 通过token查找用户身份证
     *
     * @param $token
     * @return mixed
     */    
    public static function getUidentityByToken($token)
    {
        return User::where('token', $token)
        ->leftJoin('identity', 'user.id', '=', 'identity.uid')
        ->select('user.*', 'identity.idcard','identity.name')
        ->first();
    }

    public static function getUserByTokenId($token)
    {
        $user = User::where('token' , $token)->first();
        return  $user ? $user -> id : '';

    }

    /**
     * 创建
     * @param $mobile
     */
    public static function createUser($mobile,$app_name,$terminal)
    {

        $user = User::firstOrCreate(
            [
                'userphone' => $mobile
            ],
            [
                'userphone' => $mobile,
                'terminal' => $terminal,
                'app_name' => $app_name,
                'registe_time' => DateUtils::getToday(),
                'created_ip' =>  Utils::ipAddress()
            ]
        );
        return $user;
    }


    //邀请码查找用户是否存在
    public static function getUserId($inviteCode)
    {
        $user = User::where('id',$inviteCode)->first();

        return $user? true : false ;
    }

    //修改用户的身份
    public static  function  updateIdentity($token,$identity)
    {
        $result = User::where('token',$token)->update(['identity'=>$identity]);
        return $result ? 1 : 0;
    }

    //判断用户是否冻结
    public static  function getState($mobile)
    {
        $user = User::where('userphone',$mobile)->first();

        if($user)
        {
            return ($user ->state) ? true : false;
        }

        return  true;
    }


    //通过用户id,判断用户是否被冻结
    public static  function  getStateByToken($user)
    {
        $user = User::where('token',$user)->first();

        if($user)
        {
            return ($user -> state) ? true : false;
        }

        return  true;
    }



    /*
     * 查看用户是否开启常用设备管理
     */

    public static  function getIsComDevice($userId)
    {
        $is_device = User::where('id',$userId)->first();
        return $is_device->is_device ? true : false;
    }

    //给用户表增加渠道来源
    public static  function addChannel($userId,$channel)
    {
        $data['channel'] = $channel;
        $user = User::where('id',$userId)->update($data);
        return $user ? true : false;
    }

    //修改用户身份和昵称
    public static  function setIdentity($userId,$data)
    {
        $user = User::where('id',$userId)->update($data);
        return $user ? true : false;
    }

    //增加密码错误次数
    public static  function addFrequency($userId)
    {
        $user =User::where('id',$userId)->first();
        $user -> frequency +=1;
        return ($user -> save())? true :false;
    }
    //密码错误次数超过限制,锁定账户
    public static  function lockUser($userId,$lock)
    {
        $data['lock'] =$lock;
        $result = User::where('id',$userId)->update($data);
        return $result?true : false;
    }

    //密码错误次数清零
    public static  function clearFrequency($userId)
    {
        $data['frequency'] = 0;
        $result = User::where('id',$userId)->update($data);
        return $result ? true : false;
    }
    //限制30分钟
    public static function limitUser($userId)
    {
        $key = 'anwen_user_frequency_value_' . $userId;
        return Cache::get($key)?true :false;

    }
    public static  function limitUserPut($userId)
    {
        $key = 'anwen_user_frequency_value_' . $userId;
        if (Cache::has($key)) {
            Cache::increment($key, 1);
        } else {
            Cache::put($key, 1, Carbon::now()->second(60 * 30));
        }
        return Cache::get($key);
    }

    //更新用户token
    public static  function updateToken($user_id,$data)
    {
        $result = User::where('id',$user_id)->update($data);
        return $result ? true : false;
    }
}
